package com.zbcz.crm.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.zbcz.crm.bean.Accessories
import com.zbcz.crm.databinding.ItemAccessoriesQueryBinding
import com.zbcz.crm.util.setRoundedCorners

class AccessoriesQueryAdapter(private val dataList: List<Accessories>) :
    RecyclerView.Adapter<AccessoriesQueryAdapter.AccessoriesHolder>() {

    private val expandedStates = mutableMapOf<Int, Boolean>()

    inner class AccessoriesHolder(private val binding: ItemAccessoriesQueryBinding) :
        RecyclerView.ViewHolder(binding.root) {

        init {
            binding.tvFold.setOnClickListener {
                toggleItemExpansion(adapterPosition)
            }
            binding.ivFold.setOnClickListener {
                toggleItemExpansion(adapterPosition)
            }
        }


        fun bind(accessories: Accessories) {
            binding.inContent.run {
                tvAccessoriesNameValue.text = accessories.accessoryName
                tvAccessoriesModelValue.text = accessories.accessoryCode
                tvAccessoriesTypeValue.text = accessories.accessoryType_dictText
                tvAccessoriesPurchaseValue.text = accessories.purchasePrice.toString()
                tvAccessoriesGroupPriceValue.text = accessories.groupPrice.toString()
                tvAccessoriesSellPriceValue.text = accessories.sellingPrice.toString()
                tvAccessoriesDescribeValue.text = accessories.accessoryType_dictText
                tvAccessoriesIsOnValue.text = accessories.isOn_dictText
                tvAccessoriesUnitValue.text = accessories.unit_dictText
            }
            binding.inContent.root.setRoundedCorners(45f)
        }

        fun setExpanded(expanded: Boolean) {
            val visible = if (expanded) View.VISIBLE else View.GONE
            binding.inContent.run {
                clAccessoriesType.visibility = visible
                clAccessoriesPurchasePrice.visibility = visible
                clAccessoriesGroupPrice.visibility = visible
                clAccessoriesSellPrice.visibility = visible
                clAccessoriesDescribe.visibility = visible
                clAccessoriesIsOn.visibility = visible
                clAccessoriesUnit.visibility = visible
                bottomDivider.visibility = visible
            }
        }
    }

    private fun toggleItemExpansion(position: Int) {
        expandedStates[position] = !(expandedStates[position] ?: true)
        notifyItemChanged(position)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AccessoriesHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = ItemAccessoriesQueryBinding.inflate(inflater, parent, false)
        return AccessoriesHolder(binding)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: AccessoriesHolder, position: Int) {
        holder.bind(dataList[position])
        // 设置展开或折叠的状态
        holder.setExpanded(expandedStates[position] ?: true)
    }
}